<style src="./index.scss" lang="scss" scoped rel="stylesheet/scss"></style>
<template src="./index.html"></template>
<script>
    export default {
        mixins: [Vue.prototype.$vFN.Mixins.Base],
        computed: {
            isDefault() {
                let _isDefault = false;
                this.list.forEach(item => {
                    if (item.id == this.groupId) {
                        // 是否默认分组 0否 1是
                        _isDefault = item.isDefault == 1 ? true : false;
                    }
                })
                return _isDefault;
            },
        },
        props: {
             // 使用者window实例 必定义
            appWindow: {
                type: Object,
                default: null,
            },
            // api
            albumApi: {
                type: Object,
                default: null,
            },
            type: {
                type: Number,
                default: 1,
            },

        },
        data() {
            return {
                groupId: '',
                list: [],
                form: {
                    name: "",
                },
                // 修改类型
                editType: "",
                // 窗口显示
                boxVisible: false,
            };
        },
        methods: {
            showBox(type_) {
                
                if (type_ == "update") {
                    if (this.isDefault) {
                        return
                    }
                    this.form.name = this.toItem().name;
                } else {
                    this.form.name = "";
                }
                this.boxVisible = true;
                this.editType = type_;

            },
            hideBox() {
                this.boxVisible = false;
            },
            // 初始化
            async init() {

                this.getData();

            },
            async getData() {

                let _data = await this.$vFN.Http.post({
                    url: this.albumApi.list.url,
                    domain: this.albumApi.list.domain,
                    data: {
                        type: this.type,
                    },
                },this.appWindow);
                if (_data._ok) {
                    this.list = _data.data;
                    if (this.groupId == "" && _data.data.length>0) {
                        this.change(_data.data[0].id)
                    }
                    else{
                        this.change()
                    }
                }
            },
            // 用于父组件调取刷新用
            async refresh() {
                await this.getData();
                await this.change(this.groupId);
            },
            async submit() {
                if (this.form.name == "") {
                    this.$vFN.Tools.messageError("请输入分组名称");
                    return
                }
                let _d = {
                    data: {
                        name: this.form.name,
                        type: this.type,
                    },
                }
                
                if (this.editType == "add") {
                    _d.url = this.albumApi.add.url;
                    _d.domain = this.albumApi.add.domain;
                } else {
                    _d.data.id = this.groupId
                    _d.url = this.albumApi.edit.url;
                    _d.domain = this.albumApi.edit.domain;
                }
                let _data = await this.$vFN.Http.post(_d,this.appWindow);
                if (_data._ok) {
                    this.$vFN.Tools.messageSuccess("操作成功");
                    this.getData();
                    this.hideBox();

                }
            },
            toItem() {
                let _d = null;
                this.list.forEach(item => {
                    if (item.id == this.groupId) {
                        _d = item
                    }
                })
                return _d
            },
            async del() {
                if (this.isDefault) {
                    return
                }
                let _item = this.toItem();
                console.error(_item);
                if (_item.albumCount > 0) {
                    this.$vFN.Tools.messageError("分组内存在资源，不能删除");
                    return
                }

                let _data = await this.$vFN.Http.post({
                    url :this.albumApi.del.url,
                    domain : this.albumApi.del.domain,
                    data: {

                        ids: [this.groupId],
                        type: this.type,
                    },
                },this.appWindow);
                if (_data._ok) {
                    this.$vFN.Tools.messageSuccess("删除成功");
                    this.getData();

                }
            },
            change(id) {
                this.$emit('change', id);
                this.groupId = id;

            },
        },
        watch: {},
        mounted() {
            this.init();
        },
        beforeCreate() {},
    };
</script>